package com.woniu.async;

import com.woniu.entity.UserApprove;
import com.woniu.service.impl.UserApproveServiceImpl;
import com.woniu.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @BelongsProject: phase3project
 * @BelongsPackage: com.woniu.async
 * @Author: hyl
 * @CreateTime: 2022-08-30  17:08
 * @Description: 异步，轮询数据库等方法
 * @Version: 1.0
 */
@Component
@Slf4j
public class ScheduleTasks {
    @Autowired
    private UserApproveServiceImpl userApproveService;

    /*/**
     *@description 间隔每分钟轮询数据库 超过24小时未批准的申请变更为过期状态 (方便演示设置间隔，实际可延长触发时间)
     *@params []
     *@return_value void
     *@author hyl
     *@date 2022/8/31
     */
    @Scheduled(cron = "0 * * * * *")
    void handleTimeOutUserApprove() {
        log.info("每隔一分钟轮询");
        UserApprove userApprove = new UserApprove();
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateUtils.DATETIME_FORMAT));
        String past = DateUtils.getSpecifiedMinute(now, -1440);
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.gt("user_apply_date", past);
//        wrapper.eq("user_approve_status", UserApprove.UNAPPROVED);
        userApprove.setApplyDateEnd(past);
        userApprove.setUserApproveStatus(String.valueOf(UserApprove.UNAPPROVED));
        List<UserApprove> userApproveList = userApproveService.getUserAppByAny(userApprove);
        if (userApproveList == null) {
            return;
        }
        userApproveList.forEach(e -> {
            e.setUserApproveStatus(String.valueOf(UserApprove.OUT_OF_TIME));
            e.setUserApproveFeedback("申请已过期，请重新申请");
        });
        userApproveList.forEach(e -> userApproveService.updateById(e));
    }
}
